//
//  ViewController4.m
//  ProjectA
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//
#import "ModelForViewController4.h" //M


#import "ViewForViewController4ForSectionHeader.h"
#import "CellForViewController4.h"

#import "ViewController4.h" //C
#import "ViewControllerForDestination.h"

@interface ViewController4 ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableDictionary *dicForViewController4;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ViewController4
- (void)dealloc {
 
    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
    
    [_dicForViewController4 release];
    [_tableView release];
    
    [super dealloc];
}
- (void)setNightDayModel {
    [super setNightDayModel];
    self.barBackgroundView.backgroundColor = RGBA(52, 52, 52, 1);
}
- (void)setLightDayModel {
    [super setLightDayModel];
    self.barBackgroundView.backgroundColor = RGBA(49, 211, 209, 1);
}
- (void)viewWillAppear:(BOOL)animated {
    [self.tableView reloadData];
    
   
    if (ISNIGHT) {
        [self setNightDayModel];
    } else {
        [self setLightDayModel];
    }
    
   // self.barBackgroundView.backgroundColor = [UIColor clearColor];
   
}
- (void)viewWillDisappear:(BOOL)animated {
    self.barBackgroundView.backgroundColor = [UIColor clearColor];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 加载数据菊花
  //  [self.view addSubview:[UIViewForActivityIndicator shareActivityIndicatorViewWithFrame:self.view.frame]];
    
    [self handleData];
    [self createTableView];
    [self settingsNavigationController];
}

- (void)settingsNavigationController {
    

}
- (void)handleData {
    
    self.dicForViewController4 = [NSMutableDictionary dictionary];
    [NetWorkHandler networkingWithUrl:@"http://api.breadtrip.com/destination/v3/" comleteHandle:^(id result, NSData *data) {
        NSArray *arrForElements = [result objectForKey:@"elements"];
        
        NSInteger integer1 = 0;
            for (NSDictionary *dic in arrForElements) {
                NSArray *arrForData = [dic objectForKey:@"data"];
                NSMutableArray *arr = [NSMutableArray array];
                for (NSDictionary *dic in arrForData) {
                    ModelForViewController4 *model = [[ModelForViewController4 alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    
                    [arr addObject:model];
                }
                
                NSNumber *number = [NSNumber numberWithInteger:integer1];
                [self.dicForViewController4 setObject:arr forKey:number];
                integer1++;
            }
        
        NSInteger integer2 = 0;
            for (NSDictionary *dic in arrForElements) {
                NSMutableArray *arr = [self.dicForViewController4 objectForKey:@(integer2)];
                for (ModelForViewController4 *model in arr) {
                    [model setValuesForKeysWithDictionary:dic];
                    
                }
                integer2++;
            }
        NSInteger integer3 = 0;
        NSMutableDictionary *temp = [NSMutableDictionary dictionary];
        [self.dicForViewController4 removeObjectForKey:@(4)];
        for (NSString *key in self.dicForViewController4) {
            NSMutableArray *arr = [self.dicForViewController4 objectForKey:key];
            [temp setObject:arr forKey:@(integer3)];
            integer3++;
        }
        self.dicForViewController4 = temp;
        [self.tableView reloadData];
       // [[UIViewForActivityIndicator shareActivityIndicatorViewWithFrame:self.view.frame] didFinishLoad];
        
    }];
}



- (void)createTableView {
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 30) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[ViewForViewController4ForSectionHeader class] forHeaderFooterViewReuseIdentifier:@"poolForHeader"];
    [self.tableView registerClass:[CellForViewController4 class] forCellReuseIdentifier:@"pool"];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSMutableArray *arr = [self.dicForViewController4 objectForKey:@(indexPath.section)];
    ModelForViewController4 *model = [arr objectAtIndex:indexPath.row];
    ViewControllerForDestination *destination = [[ViewControllerForDestination alloc] init];
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/", model.type, model.id];
    destination.url = url;
    destination.titleForBar = model.name;
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:destination animated:YES];
    [destination release];
    self.hidesBottomBarWhenPushed = NO;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSMutableArray *arr = [NSMutableArray array];
    for (NSInteger i = 0; i < self.dicForViewController4.allKeys.count; i++) {
        arr = [self.dicForViewController4 objectForKey:@(i)];
    }
    
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CellForViewController4 *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    cell.backgroundColor = [UIColor clearColor];
    NSArray *arr = [self.dicForViewController4 objectForKey:@(indexPath.section)];
    
    ModelForViewController4 *model = [arr objectAtIndex:indexPath.row];
    cell.parallaxRatio = 1.5f;
    [cell passData:model];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dicForViewController4.allKeys.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    ViewForViewController4ForSectionHeader *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"poolForHeader"];
    NSMutableArray *arr = [self.dicForViewController4 objectForKey:@(section)];
    ModelForViewController4 *model = [arr firstObject];
    [view passData:model];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 200;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
